/******************************************************
 * @file image_page.c
 * @author Destiny 
 * @brief 相册页面构造程序，支持jpeg、bmp等格式显示和缩放
 * @version 0.1
 * @date 2024-07-18
 *****************************************************/
/* Includes ------------------------------------------------------------------*/
#include <stdlib.h>
#include <string.h>

/* Private Includes ----------------------------------------------------------*/
#include "config.h"
#include "render.h"
#include "file.h"
#include "page_manager.h"

/* Private typedef -----------------------------------------------------------*/

/* Private Define ------------------------------------------------------------*/

/* 
     *
     *   文件显示方式
     *   --------
     *   |  图  |
     *   |  标  |
     * ------------
     * |   名字   |
     * ------------
*/
#define ZOOM_RATE  0.1
#define DIR_FILE_ICON_WIDTH 40                                            /*子目录与文件图标的宽*/
#define DIR_FILE_ICON_HEIGHT DIR_FILE_ICON_WIDTH                          /*子目录与文件图标的高*/
#define DIR_FILE_NAME_HEIGHT 20                                           /*子目录与文件名称的高*/
#define DIR_FILE_NAME_WIDTH (DIR_FILE_ICON_HEIGHT + DIR_FILE_NAME_HEIGHT) /*子目录与文件名称的宽*/
#define DIR_FILE_ALL_WIDTH DIR_FILE_NAME_WIDTH
#define DIR_FILE_ALL_HEIGHT DIR_FILE_ALL_WIDTH
#define MIN_ICON_SPACING 10 /*图标最小间距*/

#define TXT_ICON_INDEX_BASE 1000                                          /*目录文件起始索引，大于为目录文件，小于为菜单选项*/

/* Private variables ---------------------------------------------------------*/
/*菜单栏图标*/
static T_Layout g_atMenuIconsLayout[] = {
    //	{0, 0, 0, 0, "return.bmp"},
    {0, 0, 0, 0, "/opt/icon/Menu/return.bmp"},
    {0, 0, 0, 0, "/opt/icon/Menu/select.bmp"},
    {0, 0, 0, 0, "/opt/icon/Menu/shotscreen.bmp"},
    {0, 0, 0, 0, "/opt/icon/Menu/pre.bmp"},
    {0, 0, 0, 0, "/opt/icon/Menu/next.bmp"},
    {0, 0, 0, 0, "/opt/icon/Menu/delete.bmp"},
    {0, 0, 0, 0, NULL},
};

/*页面菜单栏布局*/
static T_PageLayout g_tImagePageMenuIconsLayout = {
    .iMaxTotalBytes = 0,
    .atLayout = g_atMenuIconsLayout,
};

/*不支持的图片显示图标路径与图像数据*/
static char *g_strDefaultFileIconName   = "/opt/icon/file_types/file_default.bmp"; 
static T_PixelDatas g_tDefaultFileIconPixelDatas;

/*子目录与文件图标*/
static T_Layout *g_atImageLayout;

/*页面子目录与文件布局*/
static T_PageLayout g_tImagePageImageLayout = {
    .iMaxTotalBytes = 0,
    //.atLayout       = g_atImageLayout,
};
/*每行、列的文件数*/
static int g_iDirFileNumPerCol, g_iDirFileNumPerRow;

static PT_DirContent *g_aptDirContents; /*用于存放读取的当前目录下的子目录与文件*/
static int g_iDirContentsNumber;        /*当前目录下子目录与文件的数量*/
static int g_iStartIndex = 0;           /*当前页面显示的第一个文件在所有文件中的索引*/

static char g_strCurDir[256] = IMAGE_DIR;      /*当前目录路径*/
static T_PixelDatas g_tPixelDatas;            /*文件显示区域数据*/

static float g_iCurRateOfImage = 1;      /*图片缩放比率（0.2～1）*/

/* Private Function Prototypes -----------------------------------------------*/

/**
 * @brief 计算当前目录下的文件与子目录图标位置 
*/
static void CalcImagePageImagesLayout(void)
{
    int iXres, iYres, iBpp;
    int iTopLeftX, iTopLeftY;
    int iTopLeftXBak;
    int iBotRightX, iBotRightY;
    int iIconWidth, iIconHeight;
    int iNumPerCol, iNumPerRow;
    int iDeltaX, iDeltaY;
    int i, j, k = 0;

    GetDispResolution(&iXres, &iYres, &iBpp);
    /*确定文件显示区域*/
    if (iXres < iYres)
    {
        iTopLeftX = 0;
        iBotRightX = iXres - 1;
        iTopLeftY = g_atMenuIconsLayout[0].iBotRightY + 1;
        iBotRightY = iYres - 1;
    }
    else
    {

        iTopLeftX = g_atMenuIconsLayout[0].iBotRightX + 1;
        iBotRightX = iXres - 1;
        iTopLeftY = 0;
        iBotRightY = iYres - 1;
    }
    
    /*确定图标大小*/
    iIconWidth = DIR_FILE_NAME_WIDTH;
    iIconHeight = iIconWidth;

    /*计算区域内每行可以显示多少个文件图标*/
    iNumPerRow = (iBotRightX - iTopLeftX + 1) / iIconWidth;

    /*计算得到图标间隔大小，间隔不能小于MIN_ICON_SPACING*/
    while (1)
    {
        iDeltaX = (iBotRightX - iTopLeftX + 1) - iIconWidth * iNumPerRow;
        if ((iDeltaX / (iNumPerRow + 1)) < MIN_ICON_SPACING)
            iNumPerRow--;
        else
            break;
    }

    /*计算区域内每列可以显示多少个文件图标*/
    iNumPerCol = (iBotRightY - iTopLeftY + 1) / iIconHeight;
    while (1)
    {
        iDeltaY = (iBotRightY - iTopLeftY + 1) - iIconHeight * iNumPerCol;
        if ((iDeltaY / (iNumPerCol + 1)) < MIN_ICON_SPACING)
            iNumPerCol--;
        else
            break;
    }

    iDeltaX = iDeltaX / (iNumPerRow + 1);
    iDeltaY = iDeltaY / (iNumPerCol + 1);

    g_iDirFileNumPerRow = iNumPerRow;
    g_iDirFileNumPerCol = iNumPerCol;

    /* 可以显示 iNumPerRow * iNumPerCol个"目录或文件"
     * 分配"两倍+1"的T_Layout结构体: 一个用来表示图标,另一个用来表示名字
     * 最后一个用来存NULL,借以判断结构体数组的末尾
     */
    g_atImageLayout = malloc(sizeof(T_Layout) * (2 * iNumPerRow * iNumPerCol + 1));
    if (NULL == g_atImageLayout)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"malloc error!\n");
        return;
    }
    /*确定文件显示区域大小，以及图标字节数*/
    g_tImagePageImageLayout.iTopLeftX = iTopLeftX;
    g_tImagePageImageLayout.iBotRightX = iBotRightX;
    g_tImagePageImageLayout.iTopLeftY = iTopLeftY;
    g_tImagePageImageLayout.iBotRightY = iBotRightY;
    g_tImagePageImageLayout.iBpp = iBpp;
    g_tImagePageImageLayout.atLayout = g_atImageLayout;
    g_tImagePageImageLayout.iMaxTotalBytes = DIR_FILE_ALL_WIDTH * DIR_FILE_ALL_HEIGHT * iBpp / 8;

    /*计算每个图标的显示坐标*/
    iTopLeftX += iDeltaX;
    iTopLeftY += iDeltaY;
    iTopLeftXBak = iTopLeftX;
    for (i = 0; i < iNumPerCol; i++)
    {
        for (j = 0; j < iNumPerRow; j++)
        {

            g_atImageLayout[k].iTopLeftX = iTopLeftX + (DIR_FILE_NAME_WIDTH - DIR_FILE_ICON_WIDTH) / 2;  /*左上角图标坐标*/
            g_atImageLayout[k].iBotRightX = g_atImageLayout[k].iTopLeftX + DIR_FILE_ICON_WIDTH - 1; /*右下角图标坐标*/
            g_atImageLayout[k].iTopLeftY = iTopLeftY;
            g_atImageLayout[k].iBotRightY = iTopLeftY + DIR_FILE_ICON_HEIGHT - 1;

            g_atImageLayout[k + 1].iTopLeftX = iTopLeftX; /*左上文件名坐标*/
            g_atImageLayout[k + 1].iBotRightX = iTopLeftX + DIR_FILE_NAME_WIDTH - 1;
            g_atImageLayout[k + 1].iTopLeftY = g_atImageLayout[k].iBotRightY + 1;
            g_atImageLayout[k + 1].iBotRightY = g_atImageLayout[k + 1].iTopLeftY + DIR_FILE_NAME_HEIGHT - 1;

            iTopLeftX += DIR_FILE_ALL_WIDTH + iDeltaX;
            k += 2;
        }
        iTopLeftX = iTopLeftXBak; /*计算完一行，开始新的一行*/
        iTopLeftY += DIR_FILE_ALL_HEIGHT + iDeltaY;
    }
    /*表示结尾*/
    g_atImageLayout[k].iTopLeftX = 0;
    g_atImageLayout[k].iBotRightX = 0;
    g_atImageLayout[k].iTopLeftY = 0;
    g_atImageLayout[k].iBotRightY = 0;
    g_atImageLayout[k].strIconName = NULL;
}

/**
 * @brief 为图像显示区域分配内存
 * 
 * @param ptImagePageImageLayout 页面布局
 * @return int  成功：0;失败：其他
*/
static int AllocMemForImageLayout(PT_PageLayout ptImagePageImageLayout)
{
    g_tPixelDatas.iBpp = ptImagePageImageLayout->iBpp;
    g_tPixelDatas.iWidth = ptImagePageImageLayout->iBotRightX - ptImagePageImageLayout->iTopLeftX;
    g_tPixelDatas.iHeight = ptImagePageImageLayout->iBotRightY - ptImagePageImageLayout->iTopLeftY;
    g_tPixelDatas.iLineBytes = g_tPixelDatas.iWidth * g_tPixelDatas.iBpp / 8;
    g_tPixelDatas.iTotalBytes = g_tPixelDatas.iLineBytes * g_tPixelDatas.iHeight;
    g_tPixelDatas.aucPixelDatas = malloc(g_tPixelDatas.iTotalBytes);
    if(g_tPixelDatas.aucPixelDatas == NULL)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"can't AllocMemForImageLayout");
        return -1;
    }
    return 0;
}


/**
 * @brief  获得输入事件，返回值为被选择的图标索引，用于菜单栏
 * 
 * @param ptPageLayout 页面布局区域信息
 * @param ptInputEvent 输入事件信息
 * @return int 成功：被选择的图标索引;失败：-1
*/
static int ImagePageGetInputEvent(PT_PageLayout ptPageLayout, PT_InputEvent ptInputEvent)
{
    return GenericGetInputEvent(ptPageLayout, ptInputEvent);
}

/**
 * @brief 获得输入事件，返回值为被选择的图标索引，用于文件图标
 * 
 * @param ptPageLayout 页面布局区域信息
 * @param ptInputEvent 输入事件信息
 * @return int 成功：被选择的图标索引;失败：-1
*/
static int GetInputPositionInPageLayout(PT_PageLayout ptPageLayout, PT_InputEvent ptInputEvent)
{
    int i = 0;
    PT_Layout atLayout = ptPageLayout->atLayout;

    while (atLayout[i].iBotRightY)
    {
        if ((ptInputEvent->iX >= atLayout[i].iTopLeftX) && (ptInputEvent->iX <= atLayout[i].iBotRightX) &&
            (ptInputEvent->iY >= atLayout[i].iTopLeftY) && (ptInputEvent->iY <= atLayout[i].iBotRightY))
        {

            return i;
        }
        else
        {
            i++;
        }
    }

    return -1;
}

/**
 * @brief 得到图标文件
 * 
 * @param ptPageLayout 页面子目录与文件布局
 * @return int 成功：0;失败：其他
*/
static int GenerateImageIcons(PT_PageLayout ptPageLayout)
{
    int iError;
    int iXres, iYres, iBpp;
    int iHeight,iWidth;
    PT_Layout atLayout = ptPageLayout->atLayout;
    GetDispResolution(&iXres, &iYres, &iBpp);
    iHeight = atLayout[0].iBotRightY - atLayout[0].iTopLeftY + 1;
    iWidth  = atLayout[0].iBotRightX - atLayout[0].iTopLeftX+ 1;
    iError = GetIcons(&g_tDefaultFileIconPixelDatas,g_strDefaultFileIconName,ptPageLayout->iMaxTotalBytes,iHeight,iWidth,iBpp);
    if(iError < 0)
    {   
        return -1;
    }
    return 0;
}

/**
 * @brief 生成文件与子目录图标
 * 
 * @param iStartIndex 当前页面显示的第一个文件在所有文件中的索引
 * @param iDirContentsNumber 文件总数
 * @param aptDirContents     文件目录信息
 * @param ptVideoMem         缓冲区
 * @return int 成功：0;失败：其他
*/
static int GenerateImagePageImage(int iStartIndex, int iDirContentsNumber, PT_DirContent *aptDirContents, PT_VideoMem ptVideoMem)
{
    int iError;
    int i, j, k = 0;
    int iDirContentIndex = iStartIndex;
    int iHeight,iWidth;
    char strFileName[256];
    PT_PageLayout ptPageLayout = &g_tImagePageImageLayout;
    PT_Layout atLayout = ptPageLayout->atLayout;
    T_PixelDatas tTemPixelDatas;

    iHeight = atLayout[0].iBotRightY - atLayout[0].iTopLeftY + 1;
    iWidth  = atLayout[0].iBotRightX - atLayout[0].iTopLeftX+ 1;
    (void)iError;
    /*清空矩形区域*/
    ClearRectangleInVideoMem(ptPageLayout->iTopLeftX, ptPageLayout->iTopLeftY, ptPageLayout->iBotRightX, ptPageLayout->iBotRightY, ptVideoMem, g_BackGroundColor);
    /*设置字体大小*/
    SetFontSize(atLayout[1].iBotRightY - atLayout[1].iTopLeftY - 5);

    for (i = 0; i < g_iDirFileNumPerCol; i++)
    {
        for (j = 0; j < g_iDirFileNumPerRow; j++)
        {
            /*当前文件索引小于文件数*/
            if (iDirContentIndex < iDirContentsNumber)
            {
                switch (aptDirContents[iDirContentIndex]->eFileType)
                {
 
                case FILETYPE_FILE_BMP:
                case FILETYPE_FILE_JPEG:
                case FILETYPE_FILE_PNG:
                    /*图片*/
                    /*得到图片路径*/
                    snprintf(strFileName,sizeof(strFileName),"%s/%s", g_strCurDir, g_aptDirContents[iDirContentIndex]->strName);
                    strFileName[255] = '\0';
                    DBG_PRINTF(DLOG_LVL_DEBUG,DLOG_TAG,"%s\n",strFileName);
                    iError = GetIcons(&tTemPixelDatas,strFileName,ptPageLayout->iMaxTotalBytes,iHeight,iWidth,ptPageLayout->iBpp);
                    if(iError < 0)
                    {
                         PicMerge(atLayout[k].iTopLeftX, atLayout[k].iTopLeftY, &g_tDefaultFileIconPixelDatas, &ptVideoMem->tPixelDatas);
                         break;
                    }
                    //GetPixelDatasFrmFile(strFileName,&tTemPixelDatas);
                    PicMerge(atLayout[k].iTopLeftX, atLayout[k].iTopLeftY, &tTemPixelDatas, &ptVideoMem->tPixelDatas);
                    FreePixelDatasFrmFile(&tTemPixelDatas);
                    break;
                case FILETYPE_DIR:
                case FILETYPE_FILE_TXT:  
                case FILETYPE_FILE_DEFAULT:
                    /*默认*/
                    PicMerge(atLayout[k].iTopLeftX, atLayout[k].iTopLeftY, &g_tDefaultFileIconPixelDatas, &ptVideoMem->tPixelDatas);
                    break;
                default:
                    break;
                }
                k++;

                /*显示文件名*/
                iError = MergerStringToCenterOfRectangleInVideoMem(atLayout[k].iTopLeftX, atLayout[k].iTopLeftY, atLayout[k].iBotRightX,
                                                                   atLayout[k].iBotRightY, (uint8_t *)aptDirContents[iDirContentIndex]->strName, ptVideoMem);

                k++;

                iDirContentIndex++;
            }
            else
            {
                break;
            }
        }
        if (iDirContentIndex >= iDirContentsNumber)
        {
            break;
        }
    }
    return 0;
}

/**
 * @brief 显示页面
 * 
 * @param ptPageLayout 页面布局
 * @param ptPageVideoMem 显示缓冲区
*/
static void ShowImagePage(PT_PageLayout ptPageLayout,PT_VideoMem ptVideoMem)
{
    
    int iError;
    (void)iError;

    /*图标位置计算*/
    if(!ptPageLayout->iMaxTotalBytes)
    {
        int iNumMenu = (sizeof(g_atMenuIconsLayout)/sizeof(T_Layout) - 1);
        CalculatePageMenusLayout(ptPageLayout,iNumMenu);
        CalcImagePageImagesLayout();
    }
    AllocMemForImageLayout(&g_tImagePageImageLayout);
    /* 目录和文件的图标未生成 */
    if (!g_tDefaultFileIconPixelDatas.aucPixelDatas)
    {
        GenerateImageIcons(&g_tImagePageImageLayout);
    }
    /*将数据写入缓冲区*/
    iError = GeneratePage(ptPageLayout, ptVideoMem);
    iError = GenerateImagePageImage(g_iStartIndex, g_iDirContentsNumber, g_aptDirContents, ptVideoMem);
    /*缓冲区数据写入显示设备显存*/
    FlushVideoMemToDev(ptVideoMem);
}

/**
 * @brief 图标被选择时，改变其图像数据
 * 
 * @param iDirFileIndex 索引
 * @param ptPageVideoMem 显示缓冲区
 * @param iState 被选状态 1--选择;0--释放
*/
static void SelectDirFileIcon(int iDirFileIndex,PT_VideoMem ptVideoMem)
{

    /*去掉最后一位*/
    iDirFileIndex = iDirFileIndex & ~1;
    /*得到索引*/
   
    /*文件被选中，颜色取反*/
    InvertButton(&g_atImageLayout[iDirFileIndex],ptVideoMem);
    InvertButton(&g_atImageLayout[iDirFileIndex + 1],ptVideoMem);
}

/**
 * @brief 重新加载图片图标
 * 
 * @param ptVideoMem 显示缓冲区
 * @return int 成功：0;失败：其他
*/
static int ReloadImageIcons(PT_VideoMem ptVideoMem)
{
    int iError;
    iError = GetDirContents(g_strCurDir, &g_aptDirContents, &g_iDirContentsNumber);
    if (iError)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"GetDirContents error!\n");
        return iError;
    }
    iError = GenerateImagePageImage(g_iStartIndex, g_iDirContentsNumber, g_aptDirContents, ptVideoMem);
    return iError;
}

/**
 * @brief 获取缩放后的图片数据
 * 
 * @param iRateForZoom 缩放倍率
 * @param ptZoomedImagePixelDatas 缩放后的数据
 * @return int 成功：0;失败：其他
*/
static int GetZoomedImagePiexlData(float iRateForZoom,PT_PixelDatas ptZoomedImagePixelDatas)
{
    ptZoomedImagePixelDatas->iHeight = g_tPixelDatas.iHeight * iRateForZoom;
    ptZoomedImagePixelDatas->iWidth = g_tPixelDatas.iWidth * iRateForZoom;
    ptZoomedImagePixelDatas->iBpp = g_tPixelDatas.iBpp;
    ptZoomedImagePixelDatas->iLineBytes = ptZoomedImagePixelDatas->iWidth * ptZoomedImagePixelDatas->iBpp/8;
    ptZoomedImagePixelDatas->iTotalBytes = ptZoomedImagePixelDatas->iLineBytes * ptZoomedImagePixelDatas->iHeight;
    ptZoomedImagePixelDatas->aucPixelDatas = malloc(ptZoomedImagePixelDatas->iTotalBytes);
    if (ptZoomedImagePixelDatas->aucPixelDatas == NULL)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"can't malloc memory for zoomed image!\r\n");
        return -1;
    }
    PicZoom(&g_tPixelDatas, ptZoomedImagePixelDatas);
    return 0;
}

/**
 * @brief 居中显示缩放后的图片
 * 
 * @param ptPageVideoMem 显示缓冲区
 * @param iRateForZoom 缩放比率
 * @return int 成功：0;失败：其他
*/
static int ShowZoomedImageInCentral(PT_VideoMem ptPageVideoMem,float iRateForZoom)
{
    int iError;
    int iX,iY;
    T_PixelDatas tZoomedImagePixelDatas;
    /*得到缩放后的图像数据*/
    iError = GetZoomedImagePiexlData(iRateForZoom,&tZoomedImagePixelDatas);
    if(iError < 0)
    {
        return -1;
    }
    /*计算图片中心显示时的左上角坐标*/
    iX = (g_tPixelDatas.iWidth - tZoomedImagePixelDatas.iWidth )/2 +  g_tImagePageImageLayout.iTopLeftX + 1;
    iY = (g_tPixelDatas.iHeight - tZoomedImagePixelDatas.iHeight )/2 +  g_tImagePageImageLayout.iTopLeftY + 1;

    ClearVideoMemRegion(ptPageVideoMem,&g_tImagePageImageLayout,g_BackGroundColor);

    PicMerge(iX,iY,&tZoomedImagePixelDatas,&ptPageVideoMem->tPixelDatas);

    free(tZoomedImagePixelDatas.aucPixelDatas);
    return 0;
}

/**
 * @brief 显示选中的图片
 * 
 * @param iDirFileContentIndex 图片文件索引
 * @param ptPageVideoMem 显示缓冲区
 * @return int 成功：0;失败：其他
*/
static int ShowImageInPage(int iDirFileContentIndex,PT_VideoMem ptPageVideoMem)
{
    int iError;
    char strTmp[256];
    T_PixelDatas tPixelDatastemp;
    /*判断文件类型，图片or文本*/
    switch (g_aptDirContents[iDirFileContentIndex]->eFileType)
    {
    case FILETYPE_FILE_BMP:
    case FILETYPE_FILE_JPEG:
    case FILETYPE_FILE_PNG:
    {
        snprintf(strTmp, 256, "%s/%s", g_strCurDir, g_aptDirContents[iDirFileContentIndex]->strName);
        strTmp[255] = '\0';
        strncpy(g_strCurDir, strTmp, 256);
        g_strCurDir[255] = '\0';
        iError = GetPixelDatasFrmFile(strTmp, &tPixelDatastemp);
        if (iError < 0)
        {
            DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"can't open this picture:%s\n", strTmp);
            return -1;
        }
        PicZoom(&tPixelDatastemp, &g_tPixelDatas);
        PicMerge(g_tImagePageImageLayout.iTopLeftX, g_tImagePageImageLayout.iTopLeftY, &g_tPixelDatas, &ptPageVideoMem->tPixelDatas);
        FreePixelDatasFrmFile(&tPixelDatastemp);
        break;
    }
    case FILETYPE_FILE_DEFAULT:
        /*默认*/
        break;
    default:
        break;
    }
    return 0;  
}

/**
 * @brief 数码相册逻辑
 * 
 * @param ptParentPageParams 父页面参数
*/
static void ImagePageRun(PT_PageParams ptParentPageParams)
{
    int iIndex;
    T_InputEvent tInputEvent;
    T_InputEvent tInputEventPrePress;
    int bIconPressed = 0;

    int iIndexPressed = -1;

    int iSelectFlag = 0;
    int iSelectedFileIndex[256] = {0};
    int iSelectedFileNum = 0;

    int iDirFileContentIndex = 0;
    int iHasImageBeSelected = 0;
    int iError;
    PT_VideoMem ptDevVideoMem;
    PT_VideoMem ptPageVideoMem;


    T_PageParams tPageParams;
    tPageParams.iPageID = ID("image");

    char strTmp[256];
    char *pcTmp;

    /*申请获得缓冲区*/
    ptPageVideoMem = GetVideoMem(tPageParams.iPageID, 1);
    if (ptPageVideoMem == NULL)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"can't get video mem for browse page!\n");
        return;
    }
    /*得到显示设备显存地址*/
    ptDevVideoMem = GetDevVideoMem();
    /*时间时间初始化*/
    tInputEventPrePress.tTime.tv_sec = 0;
    tInputEventPrePress.tTime.tv_usec = 0;
    
    /*读取当前目录下的文件与子目录*/
    iError = GetDirContents(g_strCurDir, &g_aptDirContents, &g_iDirContentsNumber);
    if (iError)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"GetDirContents error!\n");
        goto exit_process;
    }
    /*显示页面数据*/
    ShowImagePage(&g_tImagePageMenuIconsLayout,ptPageVideoMem);
       
    if(ptParentPageParams->iPageID == ID("browse"))
    {
        T_PixelDatas tPixelDatastemp;
        iError = GetPixelDatasFrmFile(ptParentPageParams->strCurPictureFile, &tPixelDatastemp);
        if (iError < 0)
        {
            DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"can't open this picture:%s\n", ptParentPageParams->strCurPictureFile);
            goto exit_process;
        }
        PicZoom(&tPixelDatastemp, &g_tPixelDatas);
        PicMerge(g_tImagePageImageLayout.iTopLeftX, g_tImagePageImageLayout.iTopLeftY, &g_tPixelDatas, &ptPageVideoMem->tPixelDatas);
        FreePixelDatasFrmFile(&tPixelDatastemp);
    }    
    while (1)
    {
        /*读取输入事件,获取按钮索引*/
        iIndex = ImagePageGetInputEvent(&g_tImagePageMenuIconsLayout, &tInputEvent);
        if(tInputEvent.iWheel&&iHasImageBeSelected)
        {
            if(tInputEvent.iWheel == 1)
            {
                g_iCurRateOfImage = ((g_iCurRateOfImage + ZOOM_RATE)>1) ? 1 : (g_iCurRateOfImage + ZOOM_RATE);
            }
            else
            {
                g_iCurRateOfImage = ((g_iCurRateOfImage - ZOOM_RATE) <= 0 )? g_iCurRateOfImage : (g_iCurRateOfImage - ZOOM_RATE);
            }
            DBG_PRINTF(DLOG_LVL_DEBUG,DLOG_TAG,"g_iCurRateOfImage : %f\r\n",g_iCurRateOfImage);
            ShowZoomedImageInCentral(ptPageVideoMem,g_iCurRateOfImage);
            goto mouse;
        }
        /* 触点不在菜单图标上, 则判断是在哪一个"目录和文件"上 */
        if (iIndex == -1)
        {
            iIndex = GetInputPositionInPageLayout(&g_tImagePageImageLayout, &tInputEvent);
            if (iIndex != -1)
            {
                if (g_iStartIndex + iIndex / 2 < g_iDirContentsNumber) /* 判断这个触点上是否有图标 */
                    iIndex += TXT_ICON_INDEX_BASE;
                else
                    iIndex = -1;
            }
        }
        /*按键被松开*/
        if (tInputEvent.iPressure == 0)
        {
            /*如果没有图标被按下，不做处理*/
            if (!bIconPressed)
            {
                goto mouse;
            }
            bIconPressed = 0;
            /*按下的为菜单图标*/
            if (iIndexPressed < TXT_ICON_INDEX_BASE)
            {
                //ReleaseButton(&g_atMenuIconsLayout[iIndexPressed]);
                /*按下和松开的是同一个图标*/
                if (iIndexPressed == iIndex)
                {
                    switch (iIndex)
                    {
                        case 0: /* 返回按钮 */
                        {
                            
                             InvertButton(&g_atMenuIconsLayout[iIndexPressed],ptPageVideoMem);
                             iHasImageBeSelected = 0;
                            DBG_PRINTF(DLOG_LVL_DEBUG,DLOG_TAG,"CurDir : %s\r\n",g_strCurDir);
                            if (ptParentPageParams->iPageID == ID("browse"))
                            {
                                //DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);
                                FreePixelDatasFrmFile(&g_tPixelDatas);
                                goto exit_process;
                            }
                            /*为最顶层*/
                            if (0 == strcmp(g_strCurDir, IMAGE_DIR))
                            {
                                //DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);
                                FreeDirContents(g_aptDirContents, g_iDirContentsNumber);
                                //DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);
                                FreePixelDatasFrmFile(&g_tPixelDatas);
                                //DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"%s %s %d\r\n",__FILE__,__FUNCTION__,__LINE__);
                                /*跳转到指定位置*/
                                goto exit_process;
                            }
                            else
                            {
                                pcTmp = strrchr(g_strCurDir, '/');
                                *pcTmp = '\0';
                                g_iStartIndex = 0;
                                ReloadImageIcons(ptPageVideoMem);
                            }
                            break;
                        }
                        case 1: /* "选择"按钮 */
                        {
                            if(iSelectFlag)
                            {
                                iSelectFlag = 0;
                                InvertButton(&g_atMenuIconsLayout[iIndexPressed],ptPageVideoMem);
                                while(iSelectedFileNum)
                                {
                                    iSelectedFileNum--;
                                    SelectDirFileIcon(iSelectedFileIndex[iSelectedFileNum] - TXT_ICON_INDEX_BASE,ptPageVideoMem);
                                }
                            }
                            else
                            {
                                iSelectFlag = 1;
                                InvertButton(&g_atMenuIconsLayout[iIndex],ptPageVideoMem);
                            }
                            
                            break;
                        } 
                        case 2:/* 截屏按钮 */
                        {
                            InvertButton(&g_atMenuIconsLayout[iIndexPressed],ptPageVideoMem);
                            SavePixelDataAsBmp(&ptDevVideoMem->tPixelDatas);
                            break;
                        }
                        case 3: /* "上一页" */
                        {
                            InvertButton(&g_atMenuIconsLayout[iIndexPressed],ptPageVideoMem);
                            if(iHasImageBeSelected)
                            {
                                if((iDirFileContentIndex - 1) < 0)
                                {
                                    continue;
                                }
                                else
                                {
                                    pcTmp = strrchr(g_strCurDir, '/');
                                    *pcTmp = '\0';
                                    iDirFileContentIndex--;                                
                                    ShowImageInPage(iDirFileContentIndex, ptPageVideoMem);
                                }
                                break;
                            }
                            break;
                        }
                        case 4: /* "下一页" */
                        {
                            InvertButton(&g_atMenuIconsLayout[iIndexPressed],ptPageVideoMem);
                            if(iHasImageBeSelected)
                            {
                                if((iDirFileContentIndex + 1) >= g_iDirContentsNumber)
                                {
                                    continue;
                                }
                                else
                                {
                                    pcTmp = strrchr(g_strCurDir, '/');
                                    *pcTmp = '\0';
                                    iDirFileContentIndex++;                                
                                    ShowImageInPage(iDirFileContentIndex, ptPageVideoMem);
                                }
                                break;
                            }

                        }
                        case 5: /*delete键*/
                        {
                            while(iSelectedFileNum)
                            {
                                iSelectedFileNum--;
                                iDirFileContentIndex = g_iStartIndex + (iSelectedFileIndex[iSelectedFileNum]  - TXT_ICON_INDEX_BASE) / 2;
                                snprintf(strTmp, 256, "%s/%s", g_strCurDir, g_aptDirContents[iDirFileContentIndex]->strName);
                                strTmp[255] = '\0';
                                iError = remove(strTmp);
                                if(iError < 0)
                                {
                                    DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"can't remove %s\r\n",strTmp);
                                }
                                DBG_PRINTF(DLOG_LVL_INFO,DLOG_TAG,"remove %s success!\r\n",strTmp);
                            }
                            InvertButton(&g_atMenuIconsLayout[iIndexPressed],ptPageVideoMem);
                            ReloadImageIcons(ptPageVideoMem);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
            }
            else     /*按下的是文件或目录*/
            {
                /*按下和松开的不是同一个图标*/
                if (iIndexPressed != iIndex)
                {
                    /*释放被选择文件或目录*/
                    SelectDirFileIcon(iIndexPressed - TXT_ICON_INDEX_BASE,ptPageVideoMem);
                }
                else if(iHasImageBeSelected)              
                {
                    goto mouse;
                }
                else if(iSelectFlag)
                {
                    iSelectedFileIndex[iSelectedFileNum] = iIndex;
                    iSelectedFileNum++;
                    goto mouse;
                }
                else
                {   /*单击目录进入 */
                    SelectDirFileIcon(iIndexPressed - TXT_ICON_INDEX_BASE,ptPageVideoMem);
                    iDirFileContentIndex = g_iStartIndex + (iIndexPressed - TXT_ICON_INDEX_BASE) / 2;
                    iHasImageBeSelected = 1;
                    ShowImageInPage(iDirFileContentIndex,ptPageVideoMem);
                }
            }
        }
        else     /*按键被按下*/
        {
            if (iIndex == -1)   /*事件无效*/
            {
                goto mouse;
            }
            if (!bIconPressed) /*没有文件被按下*/
            {
                bIconPressed = 1;
                iIndexPressed = iIndex;
                tInputEventPrePress = tInputEvent;
                /*菜单栏按钮被选择*/
                if (iIndex < TXT_ICON_INDEX_BASE)
                {
                    if(iIndex == 1)
                    {
                        goto mouse;
                    }
                    InvertButton(&g_atMenuIconsLayout[iIndex],ptPageVideoMem);
                }
                else
                {
                    SelectDirFileIcon(iIndex - TXT_ICON_INDEX_BASE,ptPageVideoMem);
                }
            }
            if (iIndexPressed == 0) /* 长按"向上"按钮退出*/
            {
                if (TimeMSBetween(tInputEventPrePress.tTime, tInputEvent.tTime) > 2000)
                {
                    FreeDirContents(g_aptDirContents, g_iDirContentsNumber);
                    return;
                }
            }
        }
    mouse:
        FlushVideoMemToDev(ptPageVideoMem);
        MergeMouseToDisp(&tInputEvent, ptDevVideoMem);
    }
exit_process:
    PutVideoMem(ptPageVideoMem);
}

/**
 * @brief 数码相册页面结构体
*/
static T_PageAction g_tImagePageAction = {
    .name = "image",
    .Run = ImagePageRun,
    .GetInputEvent = ImagePageGetInputEvent,
    //.Prepare       = ImagePagePrepare;
};

/* Public Function Prototypes ------------------------------------------------*/

/**
 * @brief 数码相册页面注册函数
 * 
 * @return int 成功：0;失败：其他
*/
int ImagePageInit(void)
{
    return RegisterPageAction(&g_tImagePageAction);
}